package com.atguigu.userservice.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.commonutils.ClientEntity.ClientMember;
import com.atguigu.commonutils.R;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.userservice.entity.UcenterMember;
import com.atguigu.userservice.entity.vo.LoginVo;
import com.atguigu.userservice.entity.vo.RegisterVo;
import com.atguigu.userservice.mapper.UcenterMemberMapper;
import com.atguigu.userservice.service.UcenterMemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author huangqicai
 * @since 2022-10-27
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;


    /*实现用户注册的接口*/
    @Override
    public R userRegister(RegisterVo registerVo) {
        String redisKey=null;
        /*1.获取注册信息*/
        String password = registerVo.getPassword();
        String mobile = registerVo.getMobile();
        String code = registerVo.getCode();
        if(StringUtils.isNotEmpty(mobile) && StringUtils.isNotEmpty(password)){
            /*根据手机号查询数据库，是否有该数据*/
            QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
            wrapper.eq("mobile", mobile);
            Integer integer = baseMapper.selectCount(wrapper);
            if(integer>0){
                return R.error().message("该用户手机号已经存在，请前往登录");
            }
            redisKey="redis:code:"+mobile;
        }else {
            return R.error().message("请填写好手机号或则密码");
        }
        /*2.判断手机号和手机验证码是否匹配*/
        if(StringUtils.isNotEmpty(code)){
            String redisCode = redisTemplate.opsForValue().get(redisKey).toString();
            if(StringUtils.equals(redisCode, code)){
                /*3.将注册信息保存到数据库里面*/
                UcenterMember ucenterMember = new UcenterMember();
                BeanUtils.copyProperties(registerVo, ucenterMember);
                /*密码加密*/
                String encrypt = MD5.encrypt(registerVo.getPassword());
                ucenterMember.setPassword(encrypt);
                if(StringUtils.isEmpty(registerVo.getAvatar())){
                    ucenterMember.setAvatar("https://huangedu-1010.oss-cn-chengdu.aliyuncs.com/2022/10/25/017cde506fa840ad8b91caa4ef87f200s9.jpg");
                }
                /*设置用户是活跃状态*/
                ucenterMember.setIsDisabled(false);
                int insert = baseMapper.insert(ucenterMember);
                if(insert>0){
                    return R.ok().message("用户注册成功，请前往登录！");
                }
            }else{
                return R.error().message("验证码填写错误，请核对后再填写");
            }
        }
        return R.error().message("验证码为空");
    }

    /*实现用户登录的方法*/
    @Override
    public R userLogin(LoginVo loginVo) {
        if(StringUtils.isEmpty(loginVo.getMobile()) || StringUtils.isEmpty(loginVo.getPassword())){
            throw new GuliException(20001,"请填写好你的手机号和密码");
        }
        String mobile = loginVo.getMobile();
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        if(ucenterMember==null){
            throw new GuliException(20001,"该用户没有注册，请前往注册");
        }
        if(ucenterMember.getIsDisabled()){
            throw new GuliException(20001,"该用户已经被禁用，请联系管理员");
        }
        /*先把输入的密码进行加密之后，再和数据库里面的密码进行对比*/
        if(StringUtils.equals(ucenterMember.getPassword(), MD5.encrypt(loginVo.getPassword()))){
            System.out.println("密码相等");
            /*生成token*/
            String jwtToken = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
            System.out.println("生成的token:"+jwtToken);
            /*将token存放到redis里面，并设置过期时间,这里是返回的token是放在前端的cookie里面的，由cookie来控制过期时间*/
//            redisTemplate.opsForValue().set("user:redisKey"+ucenterMember.getId(), jwtToken,30, TimeUnit.MINUTES);
            return R.ok().data("token",jwtToken);
        }else {
            System.out.println("密码不等");
            throw new GuliException(20001,"用户密码错误，请重新输入");
        }
    }



    /*根据用户id获取用户登录信息*/
    @Override
    public ClientMember getLoginInfo(String userId) {
        /*根据用户的id查询数据库，获取用户信息*/
        UcenterMember member = baseMapper.selectById(userId);
        ClientMember payMember = new ClientMember();
        BeanUtils.copyProperties(member,payMember);
        return payMember;
    }

    /*根据用户id获取用户信息，是前端传递过来的token*/
    @Override
    public UcenterMember getLoginInfoFront(String userId) {
        UcenterMember member = baseMapper.selectById(userId);
        return member;
    }


}
